//  this library.
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
// import 'package:hktk_app/util/view_util.dart';

/// 自定义返回按钮
class BackButtonWidget extends StatelessWidget {
  /// 页面上下文控制器
  final BuildContext pageContext;

  /// 返回图标
  final String svgUrl;

  /// 图标大小
  final double? size;

  /// 图标对应颜色
  final Color? color;

  /// 点击方法
  final Function()? onTapFun;

  const BackButtonWidget({
    super.key,
    required this.pageContext,
    required this.svgUrl,
    this.size,
    this.color,
    this.onTapFun,
  });

  /// 关闭按钮
  const BackButtonWidget.close({
    super.key,
    required this.pageContext,
    this.size,
    this.color,
    this.onTapFun,
  }) : svgUrl = "images/back/back_close.svg";

  /// 返回按钮
  const BackButtonWidget.back({
    super.key,
    required this.pageContext,
    this.size,
    this.color,
    this.onTapFun,
  }) : svgUrl = "images/back/back.svg";

  @override
  Widget build(BuildContext context) {
    double sizeW = size ?? 28;
    return GestureDetector(
      // HitTestBehavior.opaque：GestureDetector 会拦截其区域内的所有触摸事件，并认为其内部区域是完全不透明的，子 widget 不会接收到这些事件。
      // 相当于事件捕获并阻止默认行为
      behavior: HitTestBehavior.opaque,
      onTap: () {
        if (onTapFun != null) {
          onTapFun!();
        } else {
          Navigator.pop(pageContext);
        }
      },
      child: SizedBox(
        width: sizeW,
        height: sizeW,
        child: SvgPicture.asset(
          svgUrl,
          width: sizeW,
          height: sizeW,
          color: color,
        ),
      ),
    );
  }
}
